package com.ship.boats.business;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.ship.boats.domain.SpBoatsPortMessage;
import com.ship.boats.domain.SpBoatsReceivingStation;
import com.ship.boats.domain.SpPortInformation;
import com.ship.common.core.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * <b>UploadPortInformationBusiness</b>
 *
 * @description: UploadPortInformationBusiness <br>
 * @date: 2024/4/29 11:55 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */
@Component
public class UploadPortInformationBusiness {
    private static final Logger logger = LoggerFactory.getLogger(UploadPortInformationBusiness.class);

    public List<SpBoatsPortMessage> uploadList(MultipartFile file){
        List<SpBoatsPortMessage> portList = new ArrayList<>();
        try {
            InputStream ins = file.getInputStream();
            ExcelReader reader = ExcelUtil.getReader(ins);
            List<Map<String, Object>> maps = reader.readAll();
            // 从第三行开始读取
            AtomicInteger i = new AtomicInteger();
            maps.forEach(column -> {
                SpBoatsPortMessage boatsPortMessage = new SpBoatsPortMessage();
                if (!String.valueOf(column.get("name_cn")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("name_cn")))) {
                    boatsPortMessage.setPortNameCn(String.valueOf(column.get("name_cn")));
                }
                if (!String.valueOf(column.get("country_en")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("country_en")))) {
                    boatsPortMessage.setCountryEn(String.valueOf(column.get("country_en")));
                }
                if (!String.valueOf(column.get("country_cn")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("country_cn")))) {
                    boatsPortMessage.setCountryCn(String.valueOf(column.get("country_cn")));
                }
                if (!String.valueOf(column.get("continent_name")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("continent_name")))) {
                    boatsPortMessage.setContinentName(String.valueOf(column.get("continent_name")));
                }
                if (!String.valueOf(column.get("time_zone")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("time_zone")))) {
                    boatsPortMessage.setTimeZone(String.valueOf(column.get("time_zone")));
                }
                if (!String.valueOf(column.get("long")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("long")))) {
                    boatsPortMessage.setLon(String.valueOf(column.get("long")));
                }
                if (!String.valueOf(column.get("lat")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("lat")))) {
                    boatsPortMessage.setLat(String.valueOf(column.get("lat")));
                }
                if (!String.valueOf(column.get("type")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("type")))) {
                    boatsPortMessage.setType(String.valueOf(column.get("type")));
                }
                if (!String.valueOf(column.get("maximum_draft")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("maximum_draft")))) {
                    boatsPortMessage.setMaximumDraft(String.valueOf(column.get("maximum_draft")));
                }
                if (!String.valueOf(column.get("max_dwt")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("max_dwt")))) {
                    boatsPortMessage.setMaxDwt(String.valueOf(column.get("max_dwt")));
                }
                if (!String.valueOf(column.get("max_loa")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("max_loa")))) {
                    boatsPortMessage.setMaxLoa(String.valueOf(column.get("max_loa")));
                }
                if (!String.valueOf(column.get("max_beam")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("max_beam")))) {
                    boatsPortMessage.setMaxBeam(String.valueOf(column.get("max_beam")));
                }
                if (!String.valueOf(column.get("name_en")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("name_en")))) {
                    boatsPortMessage.setPortNameEn(String.valueOf(column.get("name_en")));
                    portList.add(boatsPortMessage);
                }
            });
        } catch (IOException e) {

            logger.error("上传数据异常,文件解析:{}", e.getMessage());
//            throw new ScreenDanceException(18002,"文件解析异常");
        } catch (Exception e) {

            logger.error("上传数据异常,程序: {}", e.getMessage());
//            throw new ScreenDanceException(18003,"文件上传异常");
        }
        return portList;
    }
}
